import { Camera, Vec3 } from 'cc';
import { UITransform } from 'cc';
import { Vec2 } from 'cc';
import { EventTouch } from 'cc';
import { _decorator, Component, Node } from 'cc';

import { Tween } from 'cc';
import { tween } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('InputController')
export class InputController extends Component {

    offset = new Vec3()
    tmpV2 = new Vec2()
    tmpV3 = new Vec3()
    startPos: Vec3;

    screenSize: Vec2
    uirect: UITransform

    scrollTween: Tween<Node>

    @property(Camera)
    camera: Camera = null

    start() {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)

        this.uirect = this.node.getComponent(UITransform)

        // var nodeUi = HzGame.instance.node.getComponent(UITransform)
        // this.screenSize = new Vec2(nodeUi.contentSize.width, nodeUi.contentSize.height)
        this.screenSize = new Vec2(720, 1280)
    }


    onTouchStart(et: EventTouch) {
        this.clearTween();
        this.tmpV2 = et.getLocation()
        this.startPos = new Vec3(this.tmpV2.x, this.tmpV2.y, 0)
        this.tmpV3 = this.camera.screenToWorld(this.startPos)
        this.startPos = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(this.tmpV3)
        Vec3.subtract(this.offset, this.startPos, this.node.position)
    }

    onTouchMove(et) {
        this.clearTween();
        this.tmpV2 = et.getLocation()
        this.tmpV3 = new Vec3(this.tmpV2.x, this.tmpV2.y, 0)
        this.tmpV3 = this.camera.screenToWorld(this.tmpV3)
        this.tmpV3 = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(this.tmpV3)
        this.tmpV3.subtract(this.offset)
        this.limitPosition(this.tmpV3)
        this.node.setPosition(this.tmpV3)
    }

    limitPosition(position: Vec3) {
        if (position.x >= this.uirect.contentSize.width / 2 - this.screenSize.x / 2) {
            position.x = this.uirect.contentSize.width / 2 - this.screenSize.x / 2
        }
        if (position.x <= -this.uirect.contentSize.width / 2 + this.screenSize.x / 2) {
            position.x = -this.uirect.contentSize.width / 2 + this.screenSize.x / 2
        }
        if (position.y >= this.uirect.contentSize.height / 2 - this.screenSize.y / 2) {
            position.y = this.uirect.contentSize.height / 2 - this.screenSize.y / 2
        }
        if (position.y <= -this.uirect.contentSize.height / 2 + this.screenSize.y / 2) {
            position.y = -this.uirect.contentSize.height / 2 + this.screenSize.y / 2
        }
    }

    scrollToPos(pos: Vec3, cb?: any) {
        this.tmpV3.set(pos);
        this.limitPosition(this.tmpV3);
        // this.node.setPosition(this.tmpV3);
        this.clearTween();
        this.scrollTween = tween(this.node)
            .to(0.1, { position: this.tmpV3 })
            .call(() => {
                cb && cb()
            })
            .start()
    }

    clearTween() {
        if (this.scrollTween) {
            this.scrollTween.stop();
            this.scrollTween = null;
        }
    }

    onTouchEnd(et) {
        this.tmpV2 = et.getLocation()
        this.tmpV3 = new Vec3(this.tmpV2.x, this.tmpV2.y, 0)
        this.tmpV3 = this.camera.screenToWorld(this.tmpV3)
        this.tmpV3 = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(this.tmpV3)


        // if (Vec3.distance(this.tmpV3,this.startPos) <= 10) {
        //     var deltaTime = game.frameStartTime - this.touchTime
        //     if (deltaTime <= 500) {

        //     }
        // }
    }

}


